<?php

namespace app\store\controller\ditui;

use app\store\controller\Controller;
use app\store\model\Store as StoreModel;
use think\Db;

class Dtuser extends Controller
{
    public function inline()
    {
        $paramArr = $this->request->param();
        $formInline = isset($paramArr['formInline']) ? $paramArr['formInline'] : "";
        $currentPage = isset($paramArr['currentPage']) ? $paramArr['currentPage'] : 1;
        $pageSize = isset($paramArr['pageSize']) ? $paramArr['pageSize'] : 10;
        $limit = " limit ".($currentPage-1)*$pageSize.", ".$pageSize;

        $order = " order by dl_id desc";
        $where = " where dl_shopid=".$this->ad_shopid." ";
        $begTimeStr = "";
        $endTimeStr = "";
        if($formInline!=""){
            trim($formInline['startTime'])!="" ? $where .= " and dl_addtime >= '".trim($formInline['startTime'])."'" : "";
            trim($formInline['endTime'])!="" ? $where .= " and dl_addtime <= '".trim($formInline['endTime'])."'" : "";
            trim($formInline['phone'])!="" ? $where .= " and du_mobile like '%".$formInline['phone']."%'" : "";
            $day_str = trim($formInline['day_str']);
            if($day_str=='today'){
                $today = date('Y-m-d');
                $begTimeStr = $today.' 00:00:00';
                $endTimeStr = $today. ' 23:59:59';
                $where .= " and dl_addtime >= '".$begTimeStr."'";
                $where .= " and dl_addtime <= '".$endTimeStr."'";
            }
            if($day_str=='yesterday'){
                // 获取昨天的日期时间对象
                $yesterday = date('Y-m-d', strtotime('-1 day'));
                $begTimeStr = $yesterday.' 00:00:00';
                $endTimeStr = $yesterday. ' 23:59:59';
                $where .= " and dl_addtime >= '".$begTimeStr."'";
                $where .= " and dl_addtime <= '".$endTimeStr."'";
            }
        }
        //ddd($where);
        $sql = "select * from ".getTable('ditui_user').$where.$order.$limit;
        $list = Db::query($sql);

        //传递总数用于分页
        $sqlNum = "select count(0) as allNum from ".getTable('ditui_user').$where;
        $allNumArr = Db::query($sqlNum);

        if ($this->request->isAjax()) {
            $back['list'] = $list;
            $back['begTimeStr'] = $begTimeStr;
            $back['endTimeStr'] = $endTimeStr;
            $back['allNum'] = $allNumArr[0]['allNum'];
            return $this->renderSuccess('数据请求成功', '', $back);
        }
        //首次非ajax请求，直接控制器推送数据
        else{
            return $this->fetch('ditui/inline', ['data' =>['list'=>$list, 'allNum'=>$allNumArr[0]['allNum']]]);
        }
    }

    /** 手动添加陌客 */
    public function addmoke()
    {
        $paramArr = $this->request->param();
        $mokeMobile = isset($paramArr['mokeMobile']) ? $paramArr['mokeMobile'] : "";
        $mokeName = isset($paramArr['mokeName']) ? $paramArr['mokeName'] : "";

        $userObj = Db::name('ditui_user')->where(['du_mobile'=>$mokeMobile, 'dl_shopid'=>$this->ad_shopid])->find();
        if($userObj){
            return $this->renderError('该手机号已经添加过了');
        }
        $inArr['dl_shopid'] = $this->ad_shopid;
        $inArr['dl_shopname'] = $this->ad_shopname;
        $inArr['dl_activetitle'] = "手动添加";
        $inArr['dl_dituiname'] = $this->admin_username.'（'.$this->admin_realname.'）';
        $inArr['wxapp_id'] = $this->wxapp_id;
        $inArr['du_mobile'] = $mokeMobile;
        $inArr['du_truename'] = $mokeName;
        $inArr['dl_addtime'] = date('Y-m-d H:i:s',time());
        Db::name('ditui_user')->insert($inArr);

        $back['state'] = 'addOk';
        $back['msg'] = '添加成功';
        return $this->renderSuccess('获取成功', '', $back);
    }

}
